class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int left, int right) {
        if(head == NULL)
        {
            return head;
        }
        if(head->next == NULL)
        {
            return head;
        }
        if(left == right)
        {
            return head;
        }
        ListNode* dummyNode = new ListNode(501, head);
        ListNode* pprev = dummyNode;
        ListNode* cur = head->next;
        ListNode* prev = head;
        int count = 1;
        ListNode* newhead = NULL;
    while (count < right)
    {
        if (count < left)
        {
            pprev = pprev->next;
            prev = prev->next;
            cur = cur->next;
            count++;
        }
        else if (count == left)
        {
            prev->next = cur->next;
            cur->next = prev;
            pprev->next = cur;
            newhead = cur;
            cur = prev->next;
            count++;
        }
        else if (count > left)
        {
            prev->next = cur->next;
            cur->next = newhead;
            pprev->next = cur;
            newhead = cur;
            cur = prev->next;
            count++;
        }
    }
        if(left == 1)
        {
            return newhead;
        }
        return head;
    }
};